<template>
  <el-dialog
    :visible.sync="visible"
    append-to-body
    width="720px"
    @close="handleClose"
  >
    <div slot="title">
      <span style="margin-right:20px">数据报告</span>
      <el-checkbox v-model="autoSend">
        自动发送报告
      </el-checkbox>
    </div>
    <div class="container">
      <el-form label-width="80px" size="mini">
        <el-form-item v-if="autoSend" label="发送时间" required>
          <el-time-select
            v-model="autoSendTime"
            placeholder="次日发送时间"
            :picker-options="{ start: '08:00', step: '00:10', end: '24:00' }"
          />
          <span class="exera">（ 时间为：周期结束后次日时间 ）</span>
        </el-form-item>
        <el-form-item label="报告标题" required>
          <el-input v-model="title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="报告内容" required>
          <template v-if="visible">
            <Editor ref="EditorRef" :height="300" />
          </template>
          <el-link type="primary" @click="handleSelectTemp">
            选择模板
          </el-link>
          <div class="tag-block">
            <template v-for="(tagItem,index) in tagListComputed">
              <span :key="index" class="tag-item" @click="()=> { handleTagSelect(tagItem) }">{{ tagItem }}</span>
            </template>
          </div>
        </el-form-item>
      </el-form>
    </div>

    <div slot="footer" class="footer">
      <el-link type="info" @click="handleTemplatePreview">
        预览
      </el-link>
      <el-link type="info" style="margin-right:20px" @click="handleTemplateSave">
        设置为模板
      </el-link>

      <el-button size="mini" @click="handleClose">
        取消
      </el-button>
      <el-button size="mini" type="primary" @click="handleOk">
        保存
      </el-button>
    </div>
    <ReportTemplateSelect ref="ReportTemplateSelectRef" />
  </el-dialog>
</template>

<script>
import Editor from '@/components/Tinymce'
import ReportTemplateSelect from './templateSelect'
import { fetchDropTaskTemplate } from '@/api/commonDropDown'
import { fetchDataReportTemplateAdd } from '@/api/dataReportTemplate'

const tagList = ['日期', '异常的下级指标']
export default {
  name: 'ReportTemplateInput',
  components: { Editor, ReportTemplateSelect },
  data() {
    return {
      visible: false,
      taskList: [],

      title: '',
      autoSend: false,
      autoSendTime: '',

      tagList //
    }
  },
  computed: {
    tagListComputed: function() {
      const { businessKpiParams } = this.$store.state.businessFormData.basicForm.data
      return [...tagList, ...businessKpiParams.map(e => e.name)]
    }
  },

  methods: {
    open(val) {
      this.$data.visible = true
      this.postTaskTemplateList()
      this.setEditor(val)

      return new Promise((resolve, reject) => {
        this.monitor = { resolve, reject }
      })
    },
    setEditor(params = {}) {
      this.$nextTick(() => {
        this.$data.title = params.title
        this.$data.reportType = params.reportType
        this.$data.autoSend = params.autoSend
        this.$data.autoSendTime = params.autoSendTime

        this.$refs.EditorRef.initSuccess().then(() => {
          this.$refs.EditorRef.setContent(params.content)
        })
      })
    },
    getEditor() {
      return this.$refs.EditorRef.getContent()
    },
    postTaskTemplateList() {
      fetchDropTaskTemplate().then(res => {
        this.$data.taskList = res
      })
    },
    postTemplateSubmit(data) {
      fetchDataReportTemplateAdd(data).then(res => {
        this.$message.success('设置成功')
      })
    },
    checkFormData(data, checkSend = true) {
      if (!data.title) {
        this.$message.warning('请输入报告标题')
        return false
      }
      if (!data.content) {
        this.$message.warning('请输入报告内容')
        return false
      }
      if (checkSend) {
        if (data.autoSend === true && !data.autoSendTime) {
          this.$message.warning('请选择报告自动发送时间')
          return false
        }
      }
      return true
    },
    handleSelectTemp() {
      this.$refs.ReportTemplateSelectRef.open().then((e) => {
        this.setEditor(e)
      })
    },
    handleTemplateSave() {
      const content = this.getEditor()
      const { title, autoSend, autoSendTime, reportType } = this.$data
      const data = { title, content, autoSend, autoSendTime, reportType }
      if (!this.checkFormData(data, false)) {
        return
      }
      this.postTemplateSubmit(data)
    },
    handleTemplatePreview() {
      // #######mark 预览功能未实现
    },
    handleTagSelect(e) {
      this.$refs.EditorRef.getEditor().insertContent('<span>@' + e + '@</span>')
    },

    handleClose() {
      this.$data.visible = false
      this.$emit('close')
    },
    handleOk() {
      const content = this.getEditor()
      const { title, autoSend, autoSendTime, reportType } = this.$data
      const data = { title, content, autoSend, autoSendTime, reportType }
      if (!this.checkFormData(data)) {
        return
      }
      this.monitor.resolve(data)
      this.handleClose()
    }

  }
}
</script>

<style lang="scss" scoped>
  .select{
    color: red;
  }
  .tag-block{
    margin-top: 20px;
    .tag-item{
      background-color: #f8f8f8;
      border: 1px solid #d9ddee;
      margin-right: 10px;
      margin-bottom: 4px;
      padding: 1px 5px;
      border-radius: 3px;
      font-size: 12px;
      cursor: pointer;
    }
  }
  .temp-block {
    margin-top: 20px;

    .title {
      margin-bottom: 10px;
      color: #666;
    }
    .temp-item {
      background-color: #fafbff;
      border: 1px solid #d9ddee;
      margin-bottom: 4px;
      padding: 5px 5px;
      border-radius: 3px;
      font-size: 12px;
      cursor: pointer;

    }
  }
  .tox-tinymce-aux {
      z-index: 11111;
  }
  .exera{
    font-size: 12px;
    color: #999;
    margin-left: 12px;
  }

</style>
